package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.epg.EpgNoticeMapper;
import com.ztgf.shopping.models.bo.epg.EpgNoticeBO;
import com.ztgf.shopping.models.bo.epg.EpgNoticeSaveBO;
import com.ztgf.shopping.models.dto.epg.EpgNoticeDTO;
import com.ztgf.shopping.models.service.epg.IEpgNoticeService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 公告消息  服务实现类
 * </p>
 *
 * @author YUE
 * @since 2020-05-20
 */
@DubboService
public class EpgNoticeServiceImpl extends ServiceImpl<EpgNoticeMapper, EpgNoticeDTO> implements IEpgNoticeService {

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private EpgNoticeMapper epgNoticeMapper;

    @Override
    public boolean add(EpgNoticeSaveBO epgNoticeSaveBO) {
        EpgNoticeDTO epgNoticeDTO = new EpgNoticeDTO();
        BeanUtils.copyProperties(epgNoticeSaveBO,epgNoticeDTO);
        Long id = ymlConfig.getPkId();
        epgNoticeDTO.setId(id);
        return this.insert(epgNoticeDTO);
    }
    /**
     * 根据相关信息查找  有页码
     * @param epgNoticeBO
     * @return
     */
    @Override
    public Page<EpgNoticeDTO> getBySelectNoticeInfoPage(EpgNoticeBO epgNoticeBO) {
        epgNoticeBO = addMustData(epgNoticeBO);
        Wrapper<EpgNoticeDTO> wrapper = getBySelectNoticeInfo(epgNoticeBO);
        return selectPage(new Page<>(epgNoticeBO.getPageNo(),epgNoticeBO.getPageSize()), wrapper);
    }

    /**
     * 添加必要数据
     * @param epgNoticeBO
     * @return
     */
    private EpgNoticeBO addMustData(EpgNoticeBO epgNoticeBO){
        epgNoticeBO.setIsPublish(1);
        epgNoticeBO.setStatus(0);
        return epgNoticeBO;
    }

    /**
     * 根据相关信息查找  无页码
     * @param epgNoticeBO
     * @return
     */
    @Override
    public List<EpgNoticeDTO> getBySelectNoticeInfoNoPage(EpgNoticeBO epgNoticeBO) {
        Wrapper<EpgNoticeDTO> wrapper = getBySelectNoticeInfo(epgNoticeBO);
        if(epgNoticeBO.getPageNo() == 0 && epgNoticeBO.getPageSize() == 0)
            return epgNoticeMapper.selectList(wrapper);
        return epgNoticeMapper.selectPage(new Page<>(epgNoticeBO.getPageNo(),epgNoticeBO.getPageSize()), wrapper);
    }

    private Wrapper<EpgNoticeDTO> getBySelectNoticeInfo(EpgNoticeBO epgNoticeBO) {
        Wrapper<EpgNoticeDTO> wrapper = new EntityWrapper<>();
        EpgNoticeDTO epgNoticeDTO = new EpgNoticeDTO();
        BeanUtils.copyProperties(epgNoticeBO,epgNoticeDTO);
        wrapper.eq("status", epgNoticeBO.getStatus());
        wrapper.eq("is_publish", epgNoticeBO.getIsPublish());
        //((EntityWrapper<EpgBannerDTO>) wrapper).setEntity(epgBannerDTO);
        return wrapper;
    }

}
